1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.reflect;
18  
19  import static com.google.common.base.Preconditions.checkNotNull;
20  
21  import java.lang.annotation.Annotation;
22  import java.lang.reflect.AccessibleObject;
23  import java.lang.reflect.Constructor;
24  import java.lang.reflect.Field;
25  import java.lang.reflect.Member;
26  import java.lang.reflect.Method;
27  import java.lang.reflect.Modifier;
28  
29  import javax.annotation.Nullable;
30  
31  /**
32   * Represents either a {@link Field}, a {@link Method} or a {@link Constructor}.
33   * Provides convenience methods such as {@link #isPublic} and {@link #isPackagePrivate}.
34   * 
35   * @author Ben Yu
36   */
37  class Element extends AccessibleObject implements Member {
38  
39    private final AccessibleObject accessibleObject;
40    private final Member member;
41  
42    <M extends AccessibleObject & Member> Element(M member) {
43      checkNotNull(member);
44      this.accessibleObject = member;
45      this.member = member;
46    }
47  
48    public TypeToken<?> getOwnerType() {
49      return TypeToken.of(getDeclaringClass());
50    }
51  
52    @Override public final boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
53      return accessibleObject.isAnnotationPresent(annotationClass);
54    }
55  
56    @Override public final <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
57      return accessibleObject.getAnnotation(annotationClass);
58    }
59  
60    @Override public final Annotation[] getAnnotations() {
61      return accessibleObject.getAnnotations();
62    }
63  
64    @Override public final Annotation[] getDeclaredAnnotations() {
65      return accessibleObject.getDeclaredAnnotations();
66    }
67  
68    @Override public final void setAccessible(boolean flag) throws SecurityException {
69      accessibleObject.setAccessible(flag);
70    }
71  
72    @Override public final boolean isAccessible() {
73      return accessibleObject.isAccessible();
74    }
75  
76    @Override public Class<?> getDeclaringClass() {
77      return member.getDeclaringClass();
78    }
79  
80    @Override public final String getName() {
81      return member.getName();
82    }
83  
84    @Override public final int getModifiers() {
85      return member.getModifiers();
86    }
87  
88    @Override public final boolean isSynthetic() {
89      return member.isSynthetic();
90    }
91  
92    /** Returns true if the element is public. */
93    public final boolean isPublic() {
94      return Modifier.isPublic(getModifiers());
95    }
96  
97    /** Returns true if the element is protected. */
98    public final boolean isProtected() {
99      return Modifier.isProtected(getModifiers());
100   }
101 
102   /** Returns true if the element is package-private. */
103   public final boolean isPackagePrivate() {
104     return !isPrivate() && !isPublic() && !isProtected();
105   }
106 
107   /** Returns true if the element is private. */
108   public final boolean isPrivate() {
109     return Modifier.isPrivate(getModifiers());
110   }
111 
112   /** Returns true if the element is static. */
113   public final boolean isStatic() {
114     return Modifier.isStatic(getModifiers());
115   }
116 
117   /**
118    * Returns {@code true} if this method is final, per {@code Modifier.isFinal(getModifiers())}.
119    * 
120    * <p>Note that a method may still be effectively "final", or non-overridable when it has no
121    * {@code final} keyword. For example, it could be private, or it could be declared by a final
122    * class. To tell whether a method is overridable, use {@link Invokable#isOverridable}.
123    */
124   public final boolean isFinal() {
125     return Modifier.isFinal(getModifiers());
126   }
127 
128   /** Returns true if the method is abstract. */
129   public final boolean isAbstract() {
130     return Modifier.isAbstract(getModifiers());
131   }
132 
133   /** Returns true if the element is native. */
134   public final boolean isNative() {
135     return Modifier.isNative(getModifiers());
136   }
137 
138   /** Returns true if the method is synchronized. */
139   public final boolean isSynchronized() {
140     return Modifier.isSynchronized(getModifiers());
141   }
142 
143   /** Returns true if the field is volatile. */
144   final boolean isVolatile() {
145     return Modifier.isVolatile(getModifiers());
146   }
147 
148   /** Returns true if the field is transient. */
149   final boolean isTransient() {
150     return Modifier.isTransient(getModifiers());
151   }
152 
153   @Override public boolean equals(@Nullable Object obj) {
154     if (obj instanceof Element) {
155       Element that = (Element) obj;
156       return getOwnerType().equals(that.getOwnerType()) && member.equals(that.member);
157     }
158     return false;
159   }
160 
161   @Override public int hashCode() {
162     return member.hashCode();
163   }
164 
165   @Override public String toString() {
166     return member.toString();
167   }
168 }